package com.stox.ui.auto.javafx;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import com.stox.dto.web.BeanDescriptor;
import com.stox.dto.web.BeanPropertyDescriptor;
import com.stox.util.StringUtil;

public class JavaFXAutoUI extends GridPane {
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JavaFXAutoUI(BeanDescriptor beanDescriptor) {
		getStyleClass().add("auto-ui-grid-pane");
		int row = 0;
		for(BeanPropertyDescriptor beanPropertyDescriptor : beanDescriptor.getProperties()){
			add(new Label(StringUtil.splitCamelCase(beanPropertyDescriptor.getName())), 0, row);
			Node node = null;
			if(Integer.class.isAssignableFrom(beanPropertyDescriptor.getType()) || Integer.TYPE.isAssignableFrom(beanPropertyDescriptor.getType())){
				TextField textField = new TextField();
				textField.getStyleClass().add("auto-ui-text-field");
				node = textField;
				textField.setText(String.valueOf(beanPropertyDescriptor.getValue()));
				textField.textProperty().addListener(new ChangeListener<String>(){
					@Override
					public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
						try{
							beanPropertyDescriptor.setValue(Integer.parseInt(newValue));
						}catch(NumberFormatException nfe){
							textField.setText(oldValue);
						}
					}
				});
			}
			
			
			if(Double.class.isAssignableFrom(beanPropertyDescriptor.getType()) || Double.TYPE.isAssignableFrom(beanPropertyDescriptor.getType())){
				TextField textField = new TextField();
				textField.getStyleClass().add("auto-ui-text-field");
				node = textField;
				textField.setText(String.valueOf(beanPropertyDescriptor.getValue()));
				textField.textProperty().addListener(new ChangeListener<String>(){
					@Override
					public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
						try{
							beanPropertyDescriptor.setValue(Double.parseDouble(newValue));
						}catch(NumberFormatException nfe){
							textField.setText(oldValue);
						}
					}
				});
			}
			
			if(beanPropertyDescriptor.getType().isEnum()){
				ChoiceBox choiseBox = new ChoiceBox();
				choiseBox.getStyleClass().add("auto-ui-choise-box");
				node = choiseBox;
				Object[] values = beanPropertyDescriptor.getType().getEnumConstants();
				choiseBox.getItems().addAll(values);
				choiseBox.getSelectionModel().select(beanPropertyDescriptor.getValue());
				choiseBox.valueProperty().addListener(new ChangeListener<Object>(){
					@Override
					public void changed(
							ObservableValue<? extends Object> observable,
							Object oldValue, Object newValue) {
						beanPropertyDescriptor.setValue(newValue);
					}
				});
			}
			add(node, 1, row);
			row++;
		}
	}
	
	
}
